package com.example.demo.rabbitmq;

import java.io.IOException;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;

@Component
public class UpMessageListener {

    @RabbitListener(bindings = {@QueueBinding
            (value = @Queue(value = "queue1", durable = "true"),
                    exchange = @Exchange(value = "MEC1", type = ExchangeTypes.TOPIC),
                    key = "test.#")})
    public void topicHandler(Message message, Channel channel) throws IOException {
        System.out.println("receive topic message -> handler1 " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

    @RabbitListener(bindings = {@QueueBinding
            (value = @Queue(value = "queue3", durable = "false"),
                    exchange = @Exchange(value = "MEC1", type = ExchangeTypes.TOPIC),
                    key = "test.#")})
    public void topicHandler4(Message message, Channel channel) throws IOException {
        System.out.println("receive topic message -> handler4 " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "queue2", durable = "true"),
            exchange = @Exchange(value = "MEC2", type = ExchangeTypes.DIRECT), key = "test.aaa")})
    public void directHandler(Message message, Channel channel) throws IOException {
        System.out.println("receive direct message -> test.aaa " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

    @RabbitListener(bindings = {@QueueBinding
            (value = @Queue(value = "queue2", durable = "true"),
                    exchange = @Exchange(value = "MEC2", type = ExchangeTypes.DIRECT),
                    key = "test.bbb")})
    public void directHandler2(Message message, Channel channel) throws IOException {
        System.out.println("receive direct message -> test.bbb " + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

}